//
//  SSMeituanRefreshView.swift
//  刷新控件
//
//  Created by 叶竞泽 on 2017/1/31.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit

let animationDuration: TimeInterval = 0.5

class SSMeituanRefreshView: SSRefreshView {
    
    override var parentHeight: CGFloat {
        didSet {
            // 15 - 126
            // 0.5 - 1
            if parentHeight < 15 {
                return
            }
            // 高度差 / 最大高度差
            // 23 = 1
            // 126 = 0
            var scale: CGFloat
            if parentHeight > 126 {
                scale = 1
            } else {
                scale = 1 - (126 - parentHeight) / (126 - 15)
            }
            kangarooIconView.transform = CGAffineTransform(scaleX: scale, y: scale) 
        }
    }
    
    var scale: CGFloat = 0.5
    
    @IBOutlet weak var buildingIconView: UIImageView!
    
    @IBOutlet weak var earthIconView: UIImageView!
    
    @IBOutlet weak var kangarooIconView: UIImageView!
    
    override func awakeFromNib() {
        
        // 1.房子
        let building1 = #imageLiteral(resourceName: "icon_building_loading_1")
        let building2 = #imageLiteral(resourceName: "icon_building_loading_2")
        
        buildingIconView.image = UIImage.animatedImage(with: [building1, building2], duration: animationDuration)
        
        // 2.地球
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = -2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 3
        animation.isRemovedOnCompletion = false
        earthIconView.layer.add(animation, forKey: nil)
        
        // 3.袋鼠
        let kangaroo1 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_1")
        let kangaroo2 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_2")
        // 设置缩放
        kangarooIconView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
        // 设置锚点
        kangarooIconView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        // 设置frame
        kangarooIconView.center = CGPoint(x: bounds.width * 0.5, y: bounds.height - 15)
        // 设置动画
        kangarooIconView.image = UIImage.animatedImage(with: [kangaroo1, kangaroo2], duration: animationDuration)
    }
    
}
